package com.gksc.video.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @className: MvcConfig
 * @description: 继承gksc-framework模板配置重写方法
 * @author: Liao.yx
 * @email: 1229053515@qq.com
 * @create: 2020年09月24日 18时59分
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 **/
@Slf4j
@EnableWebMvc
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    WebInterceptor webInterceptor;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fjc = new FastJsonConfig();
        //设置空字段输出 yml中的配置不起效  代码处理
        fjc.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
        fjc.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConverter.setFastJsonConfig(fjc);
        converters.add(0, fastJsonConverter);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (webInterceptor != null) {
            registry.addInterceptor(webInterceptor).addPathPatterns("/**");
        }
        log.info("初始化拦截器");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/","classpath:/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
